import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/16
 * Time: 03:02
 */

public class Solution {
    public static int i = 0;
    public static TreeNode createTree(int[] array) {
        TreeNode root = null;
        if (array[i] != -1){
            root = new TreeNode(array[i]);
            i++;
            root.left = createTree(array);
            root.right = createTree(array);
        }else {
            i++;
        }
        return root;
    }
    public static List<List<Integer>> levelOrder(TreeNode root){
        List<List<Integer>> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> curRow = new ArrayList<>();
            int size = queue.size();
            while(size != 0){
                TreeNode cur = queue.poll();
                if (cur != null) {
                    curRow.add(cur.val);
                }
                if (cur != null && cur.left != null) {
                    queue.add(cur.left);
                }
                if (cur != null && cur.right != null) {
                    queue.add(cur.right);
                }
                size--;
            }
            ret.add(curRow);
        }
        return ret;
    }
    public static void main(String[] args) {
        TreeNode root = createTree(new int[]{3,9,-1,-1,20,15,-1,-1,7,-1,-1});
        List<List<Integer>> lists = levelOrder(root);
        for (List<Integer> list : lists) {
            for (Integer integer : list) {
                System.out.print(integer + " ");
            }
        }
    }
}